package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.DayMp;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.MonthMp;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MonthMPStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MonthMpMapper monthMpMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void monthMPStatistics(String oId) {

        int mId = timeUtil.getMonthTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            MonthMp monthMp = monthMpMapper.selectMonthMpByDIdAndMIdAndCId(mId, menuOrderList.get(i).getCId(), menuOrderList.get(i).getMId());
            if(monthMp == null) {
                monthMp = new MonthMp();
                monthMp.setMmpId(monthMpMapper.selectCount(null) + 1);
                monthMp.setMpId(mId);
                monthMp.setMId(menuOrderList.get(i).getMId());
                monthMp.setMName(menuOrderList.get(i).getMName());
                monthMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                monthMp.setMmpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                monthMp.setMmpNum(menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(monthMp));
                monthMpMapper.insert(monthMp);
            } else {
                monthMp.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                monthMp.setMmpCalorie(monthMp.getMmpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                monthMp.setMmpNum(monthMp.getMmpNum() + menuOrderList.get(i).getMoNum());
                log.info(String.valueOf(monthMp));
                monthMpMapper.updateById(monthMp);
            }
        }
    }

}
